package seo.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataBase {
	private DataBaseHelper dbhelper;
	private SQLiteDatabase db;

	public DataBase(Context c) {
		this.dbhelper = new DataBaseHelper(c);
	}

	public void insertarPatente(String pNueva) {
		this.db = dbhelper.getWritableDatabase();
		Cursor c = this.db.rawQuery(
				"select patente from patentes where patente='" + pNueva + "'",
				null);
		if (!c.moveToFirst())
			this.db.execSQL("INSERT INTO patentes(patente) VALUES('" + pNueva
					+ "')");
		this.db.close();
	}

	public void eliminarPatente(String buscada) {
		if (buscada != null) {
			this.db = dbhelper.getWritableDatabase();
			this.db.execSQL("DELETE FROM patentes WHERE patente='" + buscada
					+ "' ");
			this.db.close();
		}
	}

	public Cursor getPatentes() {
		this.db = dbhelper.getReadableDatabase();
		Cursor c = this.db.rawQuery("select * from patentes", null);
		return c;
	}

	public String getCodigoCalle(String buscada) {
		this.db = dbhelper.getReadableDatabase();
		Cursor c = this.db.rawQuery("select codigo from calles where calle='"
				+ buscada + "'", null);
		String codigo = null;
		if (c.moveToFirst())
			codigo = c.getString(0);
		this.db.close();
		return codigo;
		
	}
	
	public void closeDataBase(){
		this.db.close();
	}

}
